/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cundong.recyclerview.sample.slice;

import com.cundong.recyclerview.HeaderAndFooterRecyclerViewAdapter;
import com.cundong.recyclerview.RecyclerViewUtils;
import com.cundong.recyclerview.ResourceTable;
import com.cundong.recyclerview.sample.bean.Contact;
import com.cundong.recyclerview.sample.provider.ContactItemProvider;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;

import java.util.ArrayList;

/**
 * DirectionalLayoutActivity Class
 */
public class DirectionalLayoutAbilitySlice extends AbilitySlice {
    private static final int LIST_ITEM_COUNT = 20;

    private ListContainer listContainer;

    private ArrayList<Contact> contactData;

    private ContactItemProvider contactItemProvider;

    private HeaderAndFooterRecyclerViewAdapter mHeaderAndFooterRecyclerViewAdapter = null;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_list_container_layout);

        Component listContainerComponent = findComponentById(ResourceTable.Id_list_container);
        if (listContainerComponent instanceof ListContainer) {
            listContainer = (ListContainer)listContainerComponent;
        }
        initContactData();
        initProvider();

        mHeaderAndFooterRecyclerViewAdapter = new HeaderAndFooterRecyclerViewAdapter(contactItemProvider);
        listContainer.setItemProvider(mHeaderAndFooterRecyclerViewAdapter);
        LayoutScatter mLayoutScatter = LayoutScatter.getInstance(this);

        RecyclerViewUtils.setHeaderView(listContainer,
            mLayoutScatter.parse(ResourceTable.Layout_header_layout, null, false));
        RecyclerViewUtils.setFooterView(listContainer,
            mLayoutScatter.parse(ResourceTable.Layout_footer_layout, null, false));
    }

    private void initProvider() {
        contactItemProvider = new ContactItemProvider(this, contactData);
    }

    private void initContactData() {
        contactData = new ArrayList<>();
        for (int index = 0; index < LIST_ITEM_COUNT; index++) {
            addContact(index);
        }
    }

    private void addContact(int index) {
        contactData.add(new Contact("User " + index, "100000000" + index));
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
